package com.jaws.core.common.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

/**
 * 字符串加解密工具类(用于身份证,银行卡等场合)
 *
 * @author jiangmy
 * @date 2017-04-14 14:34:22
 * @since v1.0.0
 */
public class CryptUtil {

	private static final byte[] ROW_BYTES = "80e36e39f34e678c".getBytes();

	public static String encrypt(String content) throws Exception {
		if (StringUtils.isBlank(content)) {
			return content;
		}
		Cipher cipher = Cipher.getInstance("AES");
		SecretKeySpec keySpec = new SecretKeySpec(ROW_BYTES, "AES");
		cipher.init(1, keySpec);
		byte[] ciphertext = cipher.doFinal(content.getBytes());
		return Base64.encodeBase64String(ciphertext);
	}

	public static String decrypt(String content) throws Exception {
		if (StringUtils.isBlank(content)) {
			return content;
		}
		Cipher cipher = Cipher.getInstance("AES");
		SecretKeySpec keySpec = new SecretKeySpec(ROW_BYTES, "AES");
		cipher.init(2, keySpec);
		byte[] ciphertext = cipher.doFinal(Base64.decodeBase64(content));
		return new String(ciphertext);
	}

}